

#include "WkJavaCall.h"
#include "../utils/WkLoger.h"
#include "../config/WkConfig.h"


WkJavaCall::WkJavaCall(_JavaVM *javaVM , JNIEnv *env , jobject*  obj )
{
    this->javaVM = javaVM ;
    this->jniEnv = env ;
    this->jobj = *obj;
    this->jobj = env->NewGlobalRef(jobj);

    jclass jcls = jniEnv->GetObjectClass(jobj) ;
    if(!jcls)
    {
        LOGE("WkJavaCall 初始化失败") ;
        return  ;
    }

    jmid_preapared = env->GetMethodID(jcls ,"onPreparedByN" ,"()V") ;
    jmid_load = env->GetMethodID(jcls ,"onLoadByN" ,"(Z)V") ;
    jmid_error = env->GetMethodID(jcls , "onErrorByN" ,"(ILjava/lang/String;)V") ;
    jmid_complate = env->GetMethodID(jcls ,"onComplateByN" , "()V") ;

    jmid_timeinfo = env->GetMethodID(jcls, "onCallTimeInfoByN", "(II)V");
    jmid_valumedb = env->GetMethodID(jcls, "onCallValumeDBByN", "(I)V");
    jmie_pcmtoaac = env->GetMethodID(jcls, "encodecPcmToAAcByN", "(I[B)V");
}

WkJavaCall::~WkJavaCall() {

}

void WkJavaCall::onPrepared(int threadType )
{
   if(threadType == MAIN_THREAD )
   {
       jniEnv->CallVoidMethod(jobj ,jmid_preapared) ;
   }
    else if(threadType == CHILD_THREAD)
   {
       JNIEnv * env ;
       if(javaVM->AttachCurrentThread(&env ,0) !=JNI_OK)
       {
           LOGE("子线程attach失败");
           return;
       }
       // 子线程 使用的 JNIEnv 和 主线程的 JNIEnv 是不一样的
       env->CallVoidMethod(jobj ,jmid_preapared) ;
       javaVM->DetachCurrentThread() ;
   }
    LOGI("子线程onPrepared  ok");
}

void WkJavaCall::onCallLoad(int threadType, bool isLoad)
{
    if(MAIN_THREAD == threadType   )
    {
        jniEnv->CallVoidMethod( jobj ,jmid_load ,isLoad) ;
    }
    else if(CHILD_THREAD == threadType)
    {
        JNIEnv * jniEvn ;
        if(javaVM->AttachCurrentThread(&jniEvn ,0) !=JNI_OK)
        {
            LOGE("on callLoad 失败");
            return;
        }
        jniEvn->CallVoidMethod(jobj , jmid_load ,isLoad) ;
        javaVM->DetachCurrentThread() ;
    }

}

void WkJavaCall::onCallError(int threadType, int code, char *msg)
{

    if(MAIN_THREAD == threadType)
    {
        jstring jmsg = jniEnv->NewStringUTF(msg) ;
        jniEnv->CallVoidMethod(jobj , jmid_error ,code ,jmsg) ;
        jniEnv->DeleteLocalRef(jmsg) ;
    }
    else if(CHILD_THREAD == threadType)
    {
        JNIEnv * jniEvn ;
        if(javaVM->AttachCurrentThread(&jniEvn ,0) !=JNI_OK)
        {
            return;
        }
        jstring jmsg = jniEnv->NewStringUTF(msg) ;
        jniEvn->CallVoidMethod(jobj ,jmid_error ,code ,jmsg) ;
        jniEvn->DeleteLocalRef(jmsg) ;
        javaVM->DetachCurrentThread();
    }

}

void WkJavaCall::onCallComplate(int threadType)
{
    if(threadType == MAIN_THREAD)
    {
        jniEnv->CallVoidMethod(jobj, jmid_complate);
    }
    else if(threadType == CHILD_THREAD)
    {
        JNIEnv *jniEnv;
        if(javaVM->AttachCurrentThread(&jniEnv, 0) != JNI_OK)
        {
            LOGE("call onCallComplete worng");
            return;
        }
        jniEnv->CallVoidMethod(jobj, jmid_complate);
        javaVM->DetachCurrentThread();
    }
}


void WkJavaCall::onCallTimeInfo(int type, int curr, int total)
{
    if(type == MAIN_THREAD)
    {
        jniEnv->CallVoidMethod(jobj, jmid_timeinfo, curr, total);
    }
    else if(type == CHILD_THREAD)
    {
        JNIEnv *jniEnv;
        if(javaVM->AttachCurrentThread(&jniEnv, 0) != JNI_OK)
        {
            LOGE("call onCallTimeInfo worng");
            return;
        }
        jniEnv->CallVoidMethod(jobj, jmid_timeinfo, curr, total);
        javaVM->DetachCurrentThread();
    }

}


void WkJavaCall::onCallValumeDB(int type, int db) {
    if(type == MAIN_THREAD)
    {
        jniEnv->CallVoidMethod(jobj, jmid_valumedb, db);
    }
    else if(type == CHILD_THREAD)
    {
        JNIEnv *jniEnv;
        if(javaVM->AttachCurrentThread(&jniEnv, 0) != JNI_OK)
        {
            LOGE("call onCallComplete worng");
            return;
        }
        jniEnv->CallVoidMethod(jobj, jmid_valumedb, db);
        javaVM->DetachCurrentThread();
    }
}


void WkJavaCall::onCallPcmToAAc(int type, int size, void *buffer) {

    if(type == MAIN_THREAD)
    {
        jbyteArray jbuffer = jniEnv->NewByteArray(size);
        jniEnv->SetByteArrayRegion(jbuffer, 0, size, static_cast<const jbyte *>(buffer));
        jniEnv->CallVoidMethod(jobj, jmie_pcmtoaac, size, jbuffer);
        jniEnv->DeleteLocalRef(jbuffer);

    }
    else if(type == CHILD_THREAD)
    {
        JNIEnv *jniEnv;
        if(javaVM->AttachCurrentThread(&jniEnv, 0) != JNI_OK)
        {
            LOGE("call onCallComplete worng");
            return;
        }
        jbyteArray jbuffer = jniEnv->NewByteArray(size);
        jniEnv->SetByteArrayRegion(jbuffer, 0, size, static_cast<const jbyte *>(buffer));
        jniEnv->CallVoidMethod(jobj, jmie_pcmtoaac, size, jbuffer);
        jniEnv->DeleteLocalRef(jbuffer);
        javaVM->DetachCurrentThread();
    }

}


